/**
 * 
 */
package servlet;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Collection;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpSessionListener;

import container.AnnotionHelper;


/**
 * @author think
 * 
 */
public abstract class AbstractServletListener implements
		ServletContextListener, ServletRequestListener, HttpSessionListener {
	private static final Logger LOGGER = Logger
			.getLogger(AbstractServletListener.class.getName());
	private static String webRoot;
	/* (non-Javadoc)
	 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
	 */
	@Override
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		ServletContext servletContext = servletContextEvent.getServletContext();
		webRoot = servletContext.getRealPath("")+File.separator;
		LOGGER.log(Level.INFO, "Server [webRoot={0}, contextPath={1}]",
	            new Object[] {webRoot, servletContextEvent.getServletContext().getContextPath()});
		try{
			AnnotionHelper annotionTest = new AnnotionHelper();
			Set<Class<?>> clazzSet = annotionTest
					.readAnnotoionClazz("net.kaixinpig");
			annotionTest.readMethodAnnotation(clazzSet);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		LOGGER.info("AbstractServletListener contextInitialized end !");
	}
	
	public static String getWebRoot(){
		return webRoot;
	}

	/*static {
		final URL resource = ClassLoader.class.getResource("/");
		if (resource != null) {
			try {
				webRoot = URLDecoder.decode(resource.getPath(), "UTF-8");
				LOGGER.log(Level.INFO, "Classpath [{0}]", webRoot);
			} catch (UnsupportedEncodingException e) {
				throw new RuntimeException("Decodes web root path failed", e);
			}
		}
	}*/
	
	

}
